package com.ctu.gzero.elements
{
	import com.ctu.gzero.utils.GzLogger;
	
	import mx.logging.*;
	import mx.logging.targets.*;
	
	public class RunElement
	{
		protected var _absoluteStart:int=0;
		protected var _relativeStart:int =0;
		protected var _textLength:int=0;
		private var _dom:DOMElement;
		private var _para:ParaElement;
		private var  _type:String = "RunElement";
		private var _id:String;
		protected var logger:ILogger;
		
		public function RunElement()
		{
			logger = GzLogger.getLogger("RunElement");
		}
		
		public function get textLength():int
		{
			return _textLength;
		}
		
		public function get id():String
		{
			return _id;
		}
		
		public function set id(val:String):void
		{
			_id = val;
		}
		
		public function get absoluteStart():int
		{
			return _absoluteStart;
		}
		
		public function set absoluteStart(val:int):void
		{
			_absoluteStart = val;
		}
		
		public function get relativeStart():int
		{
			return _relativeStart;
		}
		
		public function set relativeStart(val:int):void
		{
			_relativeStart = val;
		}
		
		public function get type():String{
			return _type;
		}
		
		public function set dom(val:DOMElement):void
		{
			_dom = val;
		}
		
		public function get dom():DOMElement
		{
			return _dom;
		}
		
		public function set para(val:ParaElement):void
		{
			_para = val;
		}
		
		public function get para():ParaElement
		{
			return _para;
		}
		
		public function normalize():void
		{
		}
		
		public function getNextRun():RunElement
		{
			var nextRun:RunElement = null;
			var idx:int = _para.getRunIndex(this);
			if(idx < _para.runList.length-1)
				nextRun = _para.getRun(idx+1);
				
			return nextRun;
		}
		
		public function getPreviousRun():RunElement
		{
			var previousRun:RunElement = null;
			var idx:int = _para.getRunIndex(this);
			if(idx > 0)
				previousRun = _para.getRun(idx-1);
				
			return previousRun;	
		}
		
		public function getNextTextLeaf(limitPara:ParaElement = null):RunElement
		{
			var nextTextLeaf:RunElement = null;
			var nextLeaf:RunElement = this;
			
			while(nextLeaf){
				nextLeaf = nextLeaf.getNextLeaf(limitPara);
				if(nextLeaf.type == "TxtRun")
				{
					nextTextLeaf = nextLeaf;
					break;
				}
			}
			
			return nextTextLeaf;
		}
		
		public function getNextLeaf(limitPara:ParaElement = null):RunElement
		{
			var nextLeaf:RunElement = null;
			var paraIdx:int = _dom.getParaIndex(_para); 
			var limitParaIdx:int = _dom.paraList.length -1; // set to last para index
			var nextPara:ParaElement = null;
			
			if(limitPara)
				limitParaIdx = _dom.getParaIndex(limitPara);

			nextLeaf = this.getNextRun();
			if(nextLeaf)
				return nextLeaf;
			
			if(paraIdx+1 > limitParaIdx)
				return null;
			
			nextPara = _para.getNextPara();
			if(nextPara == null)
				return null; 
			
			nextLeaf = nextPara.getRun(0); // first run of next para
			
			return nextLeaf;
		}
		
		public function toString():String
		{
			return _id;
		}

	}
}